#pragma once


#include<vector>
#include<string>
#include<iostream>

using namespace std;

static const int __stl_num_primes = 28;
static const unsigned long __stl_prime_list[__stl_num_primes] =
{
  53,         97,         193,       389,       769,
  1543,       3079,       6151,      12289,     24593,
  49157,      98317,      196613,    393241,    786433,
  1572869,    3145739,    6291469,   12582917,  25165843,
  50331653,   100663319,  201326611, 402653189, 805306457,
  1610612741, 3221225473, 4294967291
};

inline unsigned long __stl_next_prime(unsigned long n)
{
	const unsigned long* first = __stl_prime_list;
	const unsigned long* last = __stl_prime_list + __stl_num_primes;
	const unsigned long* pos = lower_bound(first, last, n);
	return pos == last ? *(last - 1) : *pos;
}

template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

template<>
struct HashFunc<string>
{
	size_t operator()(const string& s)
	{
		size_t sum = 0;
		for (auto& e : s)
		{
			sum *= 131;
			sum += e;
		}
		return sum;
	}
};

namespace hash
{
	enum State
	{
		EXIST,
		EMPTY,
		DELETE
	};

	template<class K, class V>
	struct HashData
	{
		pair<K, V> _kv;
		State _state = EMPTY;
	};

	template<class K, class V, class Hash = HashFunc<K>>
	class HashTable
	{
	public:
		HashTable(size_t size = __stl_next_prime(0))
			:_tables(size)
			, _n(0)
		{ }

		bool Insert(const pair<K, V> kv)
		{
			if ((double)_n / (double)_tables.size() >= 0.7)
			{
				HashTable<K, V, Hash> new_HashTable(__stl_next_prime(_tables.size() + 1));
				for (int i = 0; i < _tables.size(); i++)
				{
					if (_tables[i]._state == EXIST)
					{
						new_HashTable.Insert(_tables[i]._kv);
					}
				}
				_tables.swap(new_HashTable._tables);
			}
			Hash hs;
			size_t hash0 = hs(kv.first) % _tables.size();
			size_t hash_i = hash0;
			size_t i = 1;
			while (_tables[hash_i]._state == EXIST)
			{
				hash_i = (hash_i + i) % _tables.size();
				i++;
			}
			_tables[hash_i]._kv = kv;
			_tables[hash_i]._state = EXIST;
			_n++;

			return true;
		}

		HashData<K, V>* Find(const K& key)
		{
			Hash hs;
			size_t hash0 = hs(key) % _tables.size();
			size_t hash_i = hash0;
			size_t i = 1;
			while (_tables[hash_i]._state != EMPTY)
			{
				if (_tables[hash_i]._kv.first == key && _tables[hash_i]._state != DELETE)
				{
					return &_tables[hash_i];
				}

				hash_i = (hash_i + i) % _tables.size();
				i++;
			}
			return nullptr;
		}

		bool Erase(const K& key)
		{
			HashData<K, V>* ret = Find(key);
			if (ret)
			{
				ret->_state = DELETE;
				return true;
			}
			return false;
		}

	private:
		vector<HashData<K, V>> _tables;
		size_t _n;
	};
}


namespace yyh
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;

		HashNode(const T& data = T())
			:_data(data)
			,_next(nullptr)
		{ }
	};
	
	template<class K, class T, class KeyOft, class Hash = HashFunc<K>>
	class HashTable
	{
		typedef HashNode<T> Node;
	public:
		HashTable(size_t size = __stl_next_prime(0))
			:_tables(size, nullptr)
			, _n(0)
		{ }

		~HashTable()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
		}

		bool Insert(const T& data)
		{
			if (Find(kv.first))
			{
				return false;
			}
			Hash hs;
			if (_n == _tables.size())
			{
				vector<Node*> new_tables(__stl_next_prime(_tables.size() + 1), nullptr);
				for (int i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hash_i = hs(cur->_kv.first) % new_tables.size();
						cur->_next = new_tables[hash_i];
						new_tables[hash_i] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(new_tables);
			}
			size_t hash_i = hs(kv.first) % _tables.size();
			Node* new_node = new Node(kv);
			new_node->_next = _tables[hash_i];
			_tables[hash_i] = new_node;
			_n++;
			return true;
		}

		Node* Find(const K& key)
		{
			Hash hs;
			size_t hash_i = hs(key) % _tables.size();
			Node* cur = _tables[hash_i];
			while (cur)
			{
				if (cur->_kv.first == key)
				{
					return cur;
				}
				cur = cur->_next;
			}
			return nullptr;
		}

		bool Erase(const K& key)
		{
			Hash hs;
			size_t hash_i = hs(key) % _tables.size();
			Node* cur = _tables[hash_i];
			Node* prev = nullptr;
			while (cur)
			{
				if (cur->_kv.first == key)
				{
					if (prev == nullptr)
					{
						_tables[hash_i] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					_n--;
					delete cur;
					return true;
				}
				prev = cur;
				cur = cur->_next;
			}
			return false;
		}

	private:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}